package com.example.demo.controller;

import com.example.demo.entity.StudentCourse;
import com.example.demo.service.StudentCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/studentCourses")
@CrossOrigin
public class StudentCourseController {

    @Autowired
    private StudentCourseService studentCourseService;

    // 新增学生选课记录
    @PostMapping
    public int insertStudentCourse(@RequestBody StudentCourse studentCourse) {
        return studentCourseService.insertStudentCourse(studentCourse);
    }

    // 删除学生选课记录
    @DeleteMapping("/{id}")
    public int deleteStudentCourseById(@PathVariable int id) {
        return studentCourseService.deleteStudentCourseById(id);
    }

    // 修改学生选课记录
    @PutMapping
    public int updateStudentCourse(@RequestBody StudentCourse studentCourse) {
        return studentCourseService.updateStudentCourse(studentCourse);
    }

    // 查询学生选课记录
    @GetMapping("/{id}")
    public StudentCourse selectStudentCourseById(@PathVariable int id) {
        return studentCourseService.selectStudentCourseById(id);
    }

    // 查询所有学生选课记录
    @GetMapping
    public List<StudentCourse> selectAllStudentCourses() {
        return studentCourseService.selectAllStudentCourses();
    }

    // 联合查询学生及其选课信息
    @GetMapping("/student/{studentId}")
    public List<StudentCourse> selectStudentCoursesByStudentId(@PathVariable int studentId) {
        return studentCourseService.selectStudentCoursesByStudentId(studentId);
    }

    // 联合查询课程及其选课学生信息
    @GetMapping("/course/{courseId}")
    public List<StudentCourse> selectStudentCoursesByCourseId(@PathVariable int courseId) {
        return studentCourseService.selectStudentCoursesByCourseId(courseId);
    }
}
